package com.rags.chess.base;


public class Coordinate {
	private int row;
	private int column;
	private String notation;
	
	private static String[][] coordinateNotations = new String[8][8];
	
	static {
		String letters = "abcdefgh";
		for (int i=0; i<letters.length(); i++) {
			for (int j=0; j<letters.length(); j++)
				coordinateNotations[i][j] = String.format("%c%d", letters.charAt(i), j+1);
		}
	};
	
	public Coordinate(int aRow, int aColumn) {
		row = aRow;
		column = aColumn;
		notation = getNotation(aRow, aColumn);
	}

	public Coordinate(String aNotation) {
		notation = aNotation;
		Coordinate coord = getCoordinate(aNotation);
		row = coord.getRow();
		column = coord.getColumn();
	}

	public static String getNotation(int aRow, int aColumn) {
		return coordinateNotations[aColumn][aRow];
	}
	
	public static Coordinate getCoordinate(String aNotation) {
		char c = aNotation.charAt(0);
		int column;
		switch (c) {
			case 'a': column = 0; break;
			case 'b': column = 1; break;
			case 'c': column = 2; break;
			case 'd': column = 3; break;
			case 'e': column = 4; break;
			case 'f': column = 5; break;
			case 'g': column = 6; break;
			case 'h': column = 7; break;
			default: throw new IllegalArgumentException("Invalid column in notation");
		}
		int row = Integer.parseInt(aNotation.substring(1)) - 1;
		if (row < 0 || row > 7)
			throw new IllegalArgumentException("Invalid row in notation");
		
		return new Coordinate(row, column);
	}
	
	public int getRow() {
		return row;
	}

	public int getColumn() {
		return column;
	}
	
	public String getNotation() {
		return notation;
	}
	
	@Override
	public boolean equals(Object arg0) {
		if (arg0 instanceof Coordinate && ((Coordinate)arg0).notation.equals(notation))
			return true;
		return false;
	}
	
	@Override
	public int hashCode() {
		return notation.hashCode();
	}
	
	@Override
	public String toString() {
		return notation;
	}
}
